package MyFitnessPlate.Handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedExerciseAddCategoryDataSet;

public class ExerciseAddCategoryHandler extends DefaultHandler {

	/* <xml> <category> <id>{id}</id> <name>{name}</name>
	 * 		 </category>...
	 * </xml>
	 */
	// ===========================================================
	// Fields
	// ===========================================================
	@SuppressWarnings("unused")
	private boolean in_xml = false;
	@SuppressWarnings("unused")
	private boolean in_category = false;
	private boolean in_id = false;
	private boolean in_name = false;
	private ArrayList<ParsedExerciseAddCategoryDataSet> myParsedExerciseAddCategoryDataSet = null;

	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public ArrayList<ParsedExerciseAddCategoryDataSet> getParsedData() {
		return this.myParsedExerciseAddCategoryDataSet;
	}
	// ===========================================================
	// Methods
	// ===========================================================
	
	public void startDocument() throws SAXException {
		this.myParsedExerciseAddCategoryDataSet = new ArrayList<ParsedExerciseAddCategoryDataSet>();
	}
	
	public void endDocument() throws SAXException { /*Nothing to do*/ }

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	
	public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("category")) {
			this.in_category = true;
			myParsedExerciseAddCategoryDataSet.add(new ParsedExerciseAddCategoryDataSet());
		} else if (localName.equals("id")) {
			this.in_id = true;
		} else if (localName.equals("name")) {
			this.in_name = true;
		} 
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	
	public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("category")) {
			this.in_category = false;
		} else if (localName.equals("id")) {
			this.in_id = false;
		} else if (localName.equals("name")) {
			this.in_name = false;
		} 
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		String characterString = new String(ch, start, length);
		if (this.in_id) {
			myParsedExerciseAddCategoryDataSet.get(myParsedExerciseAddCategoryDataSet.size()-1).setId(characterString);
		}
		if (this.in_name) {
			myParsedExerciseAddCategoryDataSet.get(myParsedExerciseAddCategoryDataSet.size()-1).setCategoryName(characterString);
		}
	}
}